package org.chatboleh.bean;

import java.awt.Color;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.chatboleh.exception.EDMException;

public class TextElement extends DataObject {

	/**
	 * Version UID = 818408397318202979
	 */
	private static final long serialVersionUID = 818408397318202979L;

	// private long id;
	private String fontName;
	private int fontSize;
	private int fontStyle;
	private Color fontColor; // When saving to database, call color.getRGB() to
	// get the hex value of the color (as int). When
	// retrieve, use Color.decode("123456") to get
	// the color back.
	private String text;
	private int x;
	private int y;
	private int width;

	@Override
	protected String fieldnames() {
		return "fontName,fontSize,fontStyle,fontColor,text,x,y,width";
	}

	@Override
	public void prepareUpdate() throws EDMException {
		try {
			ps.setString(1, this.fontName);
			ps.setInt(2, this.fontSize);
			ps.setInt(3, this.fontStyle);
			ps.setInt(4, this.fontColor.getRGB());
			ps.setString(5, this.text);
			ps.setInt(6, this.x);
			ps.setInt(7, this.y);
			ps.setInt(8, this.width);
		} catch (SQLException e) {
			throw new EDMException(e);
		}
	}

	@Override
	protected String tablename() {
		return "textelement";
	}

	// public TextElement() {
	// this("Arial", Font.PLAIN, 8, Color.black, "", 0, 0, 50);
	// }

	// public TextElement(String text) {
	// this("Arial", Font.PLAIN, 8, Color.black, text, 0, 0, 50);
	// }

	public String getFontName() {
		return fontName;
	}

	public void setFontName(String fontName) {
		this.fontName = fontName;
	}

	public int getFontSize() {
		return fontSize;
	}

	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}

	public int getFontStyle() {
		return fontStyle;
	}

	public void setFontStyle(int fontStyle) {
		this.fontStyle = fontStyle;
	}

	public Color getFontColor() {
		return fontColor;
	}

	public void setFontColor(Color fontColor) {
		this.fontColor = fontColor;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public TextElement(long i) {
		super(i);
	}

	public TextElement() {
	}

	public String toString() {
		StringBuffer buff = new StringBuffer();

		Field[] fields = this.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			if (fields[i].getModifiers() == Modifier.PRIVATE) {
				try {
					buff.append(fields[i].getName()).append("\t=>\t").append(
							fields[i].get(this)).append("\n");

					// System.out.println(fields[i].getName() + " : values: " +
					// fields[i].get(this));
				} catch (IllegalAccessException ex) {
					ex.printStackTrace(System.out);
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace(System.out);
				}
			}
		}
		return buff.toString();

	}

	@Override
	protected void loadResultSetIntoObject(ResultSet resultset) {
		try {
			// while (resultset.next()) {
			this.fontName = resultset.getString("fontName");
			this.fontSize = resultset.getInt("fontSize");
			this.fontStyle = resultset.getInt("fontStyle");
			this.fontColor = Color.decode("" + resultset.getInt("fontColor"));
			this.text = resultset.getString("text");
			this.x = resultset.getInt("x");
			this.y = resultset.getInt("y");
			this.width = resultset.getInt("width");
			// }
		} catch (SQLException e) {
			logger.error(e);
		}
	}

	@Override
	public List<TextElement> getList(String criteria) throws EDMException {
		List<TextElement> list = new ArrayList<TextElement>();

		try {
			openConnection();
			prepareStatement(selectlistquery(criteria));
			executeQuery();
			while (resultset.next()) {
				TextElement te = new TextElement();
				te.id = resultset.getLong("id");
				te.loadResultSetIntoObject(resultset);
				list.add(te);
			}
		} catch (SQLException e) {
			throw new EDMException(e);
		} finally {
			closeStatement();
		}
		return list;
	}

}
